package com.llc.mall.auth.interfaces;

import com.llc.mall.auth.interfaces.vo.TokenInfoVo;
import com.llc.mall.common.security.bo.TokenInfoBO;
import com.llc.mall.auth.application.dto.RefreshTokenDTO;
import com.llc.mall.auth.application.manager.TokenStore;
import com.llc.mall.common.core.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.llc.mall.common.core.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;

/**
 * @author
 * @date 2020/6/30
 */
@RestController
@Tag(name = "token")
public class TokenController {

	@Autowired
	private TokenStore tokenStore;


	@PostMapping("/ua/token/refresh")
	public ServerResponseEntity<TokenInfoVo> refreshToken(@Valid @RequestBody RefreshTokenDTO refreshTokenDTO) {
		ServerResponseEntity<TokenInfoBO> tokenInfoServerResponseEntity = tokenStore
				.refreshToken(refreshTokenDTO.getRefreshToken());
		if (!tokenInfoServerResponseEntity.isSuccess()) {
			return ServerResponseEntity.transform(tokenInfoServerResponseEntity);
		}
		return ServerResponseEntity
				.success(BeanUtil.map(tokenInfoServerResponseEntity.getData(), TokenInfoVo.class));
	}

}
